import { ref, toRefs } from "vue"
import Query from "../../../zondy/lib/Query.js"
import { useMapStore } from "../../store/useMap.js"
const { mapState } = toRefs(useMapStore())
export const useSearchEvent = () => {
  const searchMsg = ref("")
  const searchKeyword = ref("")
  const searchEventService = { name: "traffic", layerId: 2 }
  const searchEventData = ref([])
  const eventFID = []
  function searchEvent() {
    searchEventData.value = []
    mapState.value.dialogTableVisible = true
    Query.queryByLayer({ service: searchEventService, success: searchEventSuccess })
  }
  function searchEventSuccess(e) {
    searchEventData.value = []
    e.SFEleArray.forEach(item => {
      eventFID.push(item.FID)
    })
    var format = new Zondy.Format.PolygonJSON()
    /* 将mapgis要素json反序列化为ol.feature类型数组 */
    var features = format.read(e)
    /* 将查询到的要素添加到高亮图层中 */
    features.forEach(item => {
      searchEventData.value.push(item.values_.values_)
    })
    for (let i = 0; i < searchEventData.value.length; i++) {
      searchEventData.value[i].FID = eventFID[i]
    }
    const array = []
    for (let i = 0; i < searchEventData.value.length; i++) {
      if (searchKeyword.value == "发生地点") {
        var reg = new RegExp(searchMsg.value, "i")
        var result = reg.test(searchEventData.value[i][searchKeyword.value])
        if (result) {
          array.push(searchEventData.value[i])
        }
      } else if (searchEventData.value[i][searchKeyword.value] == searchMsg.value) {
        array.push(searchEventData.value[i])
      }
    }
    mapState.value.eventData = array
    searchMsg.value = ""
  }
  return { searchMsg, searchKeyword, searchEvent }
}
